#!/usr/bin/env python3
import argparse
import pysam


def main(bamfile):
    bam = pysam.AlignmentFile(bamfile, "rb")
    for read in bam:
        if read.is_secondary:
            continue
        if read.is_qcfail:
            continue
        if read.is_unmapped:
            print(f">{read.query_name}\n{read.get_forward_sequence()}")


def run():
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument("input", type=str,
                        help="Input bam file.")
    args = parser.parse_args()
    main(args.input)


if __name__ == '__main__':
    run()
